#ifndef _STATUS_
#define _STATUS_

#include"setting.h"

class Status
{
public:
  enum Type
  {
    eNull = 0,
    eWaitting,
    eOutTime_1,
    eOutTime_2,
    eOutTime_3,
    eOutTime_4,
    eOutTime_5,
    eDrinking
  };
  enum UiType
  {
    eInit = 0,   //初始化显示
    eConnWifi,  //连接wifi显示
    eDisconnWifi, //断开wifi显示
    eTime,      //显示时间温湿度
    eDrink,     //喝水状态显示
    eSetting,   //设置参数中
    eCtling    //控制中
  };
  
  Type mType = eNull;       //当前状态
  Type mLastType = eNull;   //上次状态
  UiType mUiType;
  bool mUpdateFlag = 0;     //状态更新标志位
  bool mIsWarning = 0;
  unsigned int mSecTime = 0;         //当前状态保持时间
  unsigned int mWaitTime = 0;         //喝水后,等待的总时间

  //参数输入：0-拿起杯子，1-放下杯子
  void update(bool haveCup) //状态更新函数
  {
    mUpdateFlag = 1;
    mSecTime = 0;
    mLastType = mType;
    mIsWarning = 0; 
    if(haveCup)   //放下杯子
      mType = eWaitting;
    else         //拿起杯子
      mType = eDrinking; 
  }
  
  void advance()    //等待步进函数
  {
    static int Count_10ms = 0;
    Count_10ms ++;
    if(Count_10ms < 10)
      return ;
    Count_10ms = 0;
    mSecTime ++;
    mWaitTime ++;
    mUpdateFlag = 0;
    control();
  }
  
  void control()    //状态控制函数
  {
    if(mType == eWaitting)  //等待状态
    {
      if(mSecTime > setting.drink_waitTime * 60)
      {
        mType = eOutTime_1;
        mSecTime = 0;
        mWaitTime = 0;
        mUpdateFlag = 1;
        mIsWarning = 1;
      }  
    }
    else if(mType == eOutTime_1)  //超时状态 1
    {
      if(mSecTime > setting.drink_out1Time * 60)
      {
        mType = eOutTime_2;
        mSecTime = 0;
        mUpdateFlag = 1;
      }  
    }
    else if(mType == eOutTime_2)  //超时状态 2  
    {
      if(mSecTime > setting.drink_out2Time * 60)
      {
        mType = eOutTime_3;
        mSecTime = 0;
        mUpdateFlag = 1;
      }  
    }
    else if(mType == eOutTime_3)  //超时状态 3
    {
      if(mSecTime > setting.drink_out3Time * 60)
      {
        mType = eOutTime_5;
        mSecTime = 0;
        mUpdateFlag = 1;
      }  
    }
    else if(mType == eOutTime_4)  //超时状态 4
    {
      if(mSecTime > setting.drink_out4Time * 60)
      {
        mType = eOutTime_5;
        mSecTime = 0;
        mUpdateFlag = 1;

        setting.ui_period = 30;
        setting.ui_switch = 10;
      }
    }
  }
};

Status state;           //状态对象
#endif
